﻿/*  ----------------------------------------------------------------------------
 *  SysServer
 *  ----------------------------------------------------------------------------
 *  File:       TaskContext.cs
 *  Creater:     SEEN\MistSeen
 *  ----------------------------------------------------------------------------
 *  $Date: 2010-09-06 10:36:01 +0000 (Mon, 06 Sep 2010) $
 *  $Rev: 15 $
 *  $Author: ScorDeity $
 *  $URL: http://sysserver.googlecode.com/svn/trunk/src/SysServer/TaskContext.cs $
 *  $Id: TaskContext.cs 15 2010-09-06 10:36:01Z ScorDeity $
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace SysServer
{
    public interface IContextIdGenerator
    {
        object NextId { get; }
        void Reset();
    }

    public interface IContext
    {
        object Id { get; }

        void Lock();
        void UnLock();
    }

    public interface IContextFactory
    {
        IContext CreateCotnext();
    }

    public class ContextIdGenerator : IContextIdGenerator
    {
        private static readonly ContextIdGenerator _ctxIdGen = InnerIdGen.InnerCtxIdGen;
        private static long _ctxId = 0;

        public static IContextIdGenerator Instance
        {
            get { return _ctxIdGen; }
        }
        static class InnerIdGen
        {
            static InnerIdGen()
            {
                InnerCtxIdGen = new ContextIdGenerator();
            }

            public static readonly ContextIdGenerator InnerCtxIdGen;
        }



        #region IContextIdGenerator 成员

        public object NextId
        {
            get { return Interlocked.Increment(ref _ctxId); }
        }

        public void Reset()
        {
            Interlocked.Exchange(ref _ctxId, 0);
        }

        #endregion
    }

    public class ContextFacotry : IContextFactory
    {
        private IContextIdGenerator _ctxIdGen;

        public IContextIdGenerator ctxIdGen
        {
            get
            {
                return _ctxIdGen;
            }
            set
            {
                _ctxIdGen = value;
            }
        }


        public ContextFacotry(IContextIdGenerator idGen)
        {
            this._ctxIdGen = idGen;
        }

        #region IContextFactory 成员

        public IContext CreateCotnext()
        {
            return new Context(this._ctxIdGen.NextId);
        }

        #endregion
    }

    public class Context : IContext
    {
        public Context(object id)
        {
            this._Id = id;
        }

        private object _Id;

        public object Id
        {
            get
            {
                return _Id;
            }
            set
            {
                _Id = value;
            }
        }




        #region IContext 成员


        public void Lock()
        {
            Monitor.Enter(this);
        }

        public void UnLock()
        {
            Monitor.Exit(this);
        }

        #endregion
    }
}